﻿using System.Windows.Forms;
using System.Drawing;

namespace XFControls.XFPanels
{
    public partial class XFPanelScrollbar : Panel
    {
        public Color BarColor { get; set; }
        public int xOffset { get; set; }
        private double percentage;
        private Point _baseTop;

        public XFPanelScrollbar()
        {
            InitializeComponent();
            BarColor = Color.Black;
        }

        public void UpdatePanel(object sender)
        {
            XFPanelBase obj = (XFPanelBase)sender;

            if (obj.Height == 0)
                return;

            percentage = (double)ClientSize.Height / obj.Height;

            int pPos = obj.VirtualLocation.Y * -1;
            double pPerc = (double)pPos / obj.Height;
            _baseTop = new Point(xOffset - 5, (int)(pPerc * ClientSize.Height));
        }

        protected override void OnPaint(PaintEventArgs e) { }

        public void PaintScroll(Graphics g)
        {
            int h = (int)(Height * percentage) + 1;
            Rectangle r = new Rectangle(_baseTop.X, _baseTop.Y, Width, h);
            using (SolidBrush b = new SolidBrush(BarColor))
                g.FillRectangle(b, r);
        }

        protected override void OnPaintBackground(PaintEventArgs e) { }
    }
}
